package wiki;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.LoginChecks;

import javax.servlet.http.Cookie;
import java.util.UUID;


/**
 * Servlet implementation class VerifyLogin
 */
public class VerifyLogin extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public VerifyLogin() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("application/json");
		PrintWriter out = response.getWriter();

		String loginstatus = "failed";
		System.out.println("VerifyLogin");
		
		boolean prod = true;

		try {
			String username = null; 
			String password = null;

			boolean logintest =false;

			HttpSession session =null;
			
			if (prod == false) {
				//System.out.println("DEV ENV");
				logintest = true;
				
				session = request.getSession(true);
				session.setAttribute("firstname", "Mona");
				session.setAttribute("userrole", "2");
				session.setAttribute("user", "Mona Chandrapal");
				session.setAttribute("corpuser", "mona.chandrapal");
				session.setAttribute("isActive", true);
				
								
				loginstatus = "success";
				
			} else {
				
				username = request.getParameter("username");
				password = request.getParameter("password");

				
				
				LoginChecks checklogindetail = new LoginChecks();
				logintest = checklogindetail
						.loginvalidation(username, password);

				
				if (logintest == true) {

					checklogindetail.setuser(username);
					session = request.getSession(true);
					session.setAttribute("isActive", true);
					session.setAttribute("username", username);
					// System.out.println("Login credentials valid");
					
					
					
					String userrole = checklogindetail
							.accessrightsvalidation(username);
					
					String cuser=username;
					String cpwd= password;
					
					String[] uname = username.split("\\.");
					String ufname = uname[0];
					String ulname = uname[1];

					ufname = ufname.substring(0, 1).toUpperCase()
							+ ufname.substring(1);
					ulname = ulname.substring(0, 1).toUpperCase()
							+ ulname.substring(1);

					String corpuser = ufname+ulname;
					
					String user = ufname + ulname;
					session.setAttribute("firstname", ufname);
					session.setAttribute("userrole", userrole);
					session.setAttribute("user", user);
					session.setAttribute("corpusername", corpuser);
					loginstatus = "success";
					//System.out.println("Corpuser: " +corpuser);
					
				/*	
					String remember = "0";
					if(remember.equalsIgnoreCase("1")){
				           //create a token using the Universal Unique Identifier Generator
				           //String cookieToken=uid.randomUUID();
				           //Create the cookie
				           Cookie cookie=new Cookie("ITUser",cuser);
				           Cookie cookiepw = new Cookie("ITPWD", cpwd);
				           //Set cookie's life to..forever
				           cookie.setMaxAge(365 * 24 * 60 * 60);
				           cookiepw.setMaxAge(365 * 24 * 60 * 60);
				           
				           //cookie to the response

				           response.addCookie(cookie);
				           response.addCookie(cookiepw);
				           
				           
				           System.out.println("Reading cookies");

				           Cookie [] t1= null;
				           t1 = request.getCookies();
				           for (int i = 0; i < t1.length; i++){
				               cookie = t1[i];
				               System.out.println("Name : " + cookie.getName( ) + ",  ");
				               System.out.println("Value: " + cookie.getValue( )+" <br/>");
				            }	   
				        }
					
					*/
					
					response.sendRedirect("wiki/home.jsp");

				} else {
					loginstatus = "failed";
					response.sendRedirect("login.jsp?stat=failed");
				}

			}

		} catch (Exception e) {
			System.out
					.println("Exception when verifying login " + e.toString());
			
		}

		out.println("{\"loginstatus\": [{\"status\": \"" + loginstatus
				+ "\"}]}");

		
	}

}
